import { ACTION_TYPES } from 'utils/constant';
const initialState = {
    loading: false,
    isLogin: '...',
    user: {

    }
}

export default function login(state = initialState, action) {
    switch (action.type) {
        case ACTION_TYPES.LOGIN_REQUEST:
            return ({
                ...state,
                loading: action.loading,
                isLogin: action.isLogin
                
            })
        case ACTION_TYPES.LOGIN_SUCCESS:
            return ({
                ...state,
                loading: action.loading,
                isLogin: action.isLogin
            })
        case ACTION_TYPES.LOGIN_FAIL:
            return ({
                ...state,
                loading: action.loading,
                isLogin: action.isLogin
                
            })
        case ACTION_TYPES.GET_USER_INFO_REQUEST:
            return ({
                ...state,
                loading: action.loading,          
            })
        case ACTION_TYPES.GET_USER_INFO_SUCCESS:
            return ({
                ...state,
                loading: action.loading,
                user: action.user,
                isLogin: action.isLogin
            })
        case ACTION_TYPES.GET_USER_INFO_FAIL:
            return ({
                ...state,
                loading: action.loading,
                isLogin: action.isLogin
            })
        case ACTION_TYPES.USER_GROUP_UPDATE_SUCCESS: {
            const groups = state.user.groups;
            const index = groups.findIndex(item => item.members.findIndex(i => i === action.id) > -1);
            if (index > -1) {
                groups.splice(index, 1);
            }
            return ({
                ...state,
                user: {
                    ...state.user,
                    groups
                }
                
            })
        }
        default:
            return state;
    }
}



